﻿
Imports std

Namespace internalFunctions
    'parameters, file, any, convert to string
    '            content, pointer<string> / pointer<var>
    'return, bool, success or not
    Public Class readfile
        Inherits internalFunction

        Public Class parameters
            Public Const file As String = "file"
            Public Const content As String = "content"
        End Class

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.file, parameters.content)
        End Sub

        Public Sub New()
            MyBase.New("file.read", m)
        End Sub

        Protected Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
            Dim file As String = Nothing
            If ctx.findVariableStr(parameters.file, file) Then
                Dim content As bare_variable = Nothing
                If ctx.findVariable(parameters.content, content) Then
                    If content.variable().isPointerOfString() Then
                        Dim txt As String = Nothing
                        output = New bare_variable(typeDefination.bool, _
                                                   Global.readfile(file, txt) AndAlso content.setPointerOfString(txt))
                        Return True
                    Else
                        raiseError("expect parameter " + parameters.content + " is pointer<string> or pointer<var>.", _
                                    errorHandle.errorType.user)
                        Return False
                    End If
                Else
                    raiseError("expect parameter " + parameters.content, errorHandle.errorType.user)
                    Return False
                End If
            Else
                raiseError("expect parameter " + parameters.file, errorHandle.errorType.user)
                Return False
            End If
        End Function
    End Class

    'parameters, file, any, convert to string
    '            content, pointer<string> / pointer<var>
    'return, bool, success or not
    Public Class readfilebytes
        Inherits internalFunction

        Public Class parameters
            Public Const file As String = "file"
            Public Const buff As String = "buff"
        End Class

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.file, parameters.buff)
        End Sub

        Public Sub New()
            MyBase.New("file.read.bytes", m)
        End Sub

        Protected Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
            Dim file As String = Nothing
            If ctx.findVariableStr(parameters.file, file) Then
                Dim content As bare_variable = Nothing
                If ctx.findVariable(parameters.buff, content) Then
                    If content.variable().isPointerOfVar() Then
                        Dim buff As bytes = Nothing
                        output = New bare_variable(typeDefination.bool, _
                                                   Global.readfile(file, buff) AndAlso content.setPointerOfVar(buff))
                        Return True
                    Else
                        raiseError("expect parameter " + parameters.buff + " is pointer<var>.", _
                                    errorHandle.errorType.user)
                        Return False
                    End If
                Else
                    raiseError("expect parameter " + parameters.buff, errorHandle.errorType.user)
                    Return False
                End If
            Else
                raiseError("expect parameter " + parameters.file, errorHandle.errorType.user)
                Return False
            End If
        End Function
    End Class

    'parameters, file, any, convert to string
    'return, bool, whether the file is existing or not
    Public Class fileexists
        Inherits internalFunction

        Public Class parameters
            Public Const file As String = "file"
        End Class

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.file)
        End Sub

        Public Sub New()
            MyBase.New("file.exist", m)
        End Sub

        Protected Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
            Dim file As String = Nothing
            If ctx.findVariableStr(parameters.file, file) Then
                output = New bare_variable(typeDefination.bool, IO.File.Exists(file))
                Return True
            Else
                raiseError("expect parameter " + parameters.file, errorHandle.errorType.user)
                Return False
            End If
        End Function
    End Class

    'parameters, file, any, convert to string
    '            content, any, convert to string
    'return, bool, success or not
    Public Class writefile
        Inherits internalFunction

        Public Class parameters
            Public Const file As String = "file"
            Public Const content As String = "content"
        End Class

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.file, parameters.content)
        End Sub

        Public Sub New()
            Me.New("file.write")
        End Sub

        Protected Sub New(ByVal name As String)
            MyBase.New(name, m)
        End Sub

        Protected Overridable Overloads Function invoke(ByVal file As String, ByVal content As String) As Boolean
            Return Global.writefile(file, content)
        End Function

        Protected NotOverridable Overrides Function invoke(ByVal ctx As context, _
                                                           ByRef output As bare_variable) As Boolean
            Dim file As String = Nothing
            If ctx.findVariableStr(parameters.file, file) Then
                Dim content As String = Nothing
                If ctx.findVariableStr(parameters.content, content) Then
                    output = New bare_variable(typeDefination.bool, invoke(file, content))
                    Return True
                Else
                    raiseError("expect parameter " + parameters.content, errorHandle.errorType.user)
                    Return False
                End If
            Else
                raiseError("expecte parameter " + parameters.file, errorHandle.errorType.user)
                Return False
            End If
        End Function
    End Class

    Public Class appendfile
        Inherits writefile

        Public Sub New()
            MyBase.New("file.append")
        End Sub

        Protected Overrides Function invoke(ByVal file As String, ByVal content As String) As Boolean
            Return Global.appendfile(file, content)
        End Function
    End Class

    'parameters, src, any, convert to string, source file
    '            dest, any, convert to string, destination file
    '            overwrite, bool, optional, default false, whether overwrite existing file
    'return, bool, success or not
    Public Class copyfile
        Inherits internalFunction

        Public Class parameters
            Public Const src As String = "src"
            Public Const dest As String = "dest"
            Public Const overwrite As String = "overwrite"
        End Class

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.src, parameters.dest, parameters.overwrite)
        End Sub

        Public Sub New()
            MyBase.New("file.copy", m)
        End Sub

        Protected Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
            Dim src As String = Nothing
            If ctx.findVariableStr(parameters.src, src) Then
                Dim dest As String = Nothing
                If ctx.findVariableStr(parameters.dest, dest) Then
                    Dim overwrite As Boolean
                    If Not ctx.findVariableBool(parameters.overwrite, overwrite) Then
                        overwrite = False
                    End If

                    Dim rtn As Boolean = True
                    Try
                        IO.File.Copy(src, dest, overwrite)
                    Catch
                        rtn = False
                    End Try

                    output = New bare_variable(typeDefination.bool, rtn)
                    Return True
                Else
                    raiseError("expect parameter " + parameters.dest, errorHandle.errorType.user)
                    Return False
                End If
            Else
                raiseError("expect parameter " + parameters.src, errorHandle.errorType.user)
                Return False
            End If
        End Function
    End Class

    'parameters, i, any, convert to string, the source of directory to list
    'return, vector<string>, the files & directories under dir
    Public Class listdirectory
        Inherits inputIFunction(Of String, pointer(Of vector(Of String)))

        Public Sub New()
            MyBase.New("directory.list")
        End Sub

        Protected Overrides Function invoke(ByVal i As String, ByRef o As pointer(Of vector(Of String))) As Boolean
            If o Is Nothing Then
                o = New elpmis_vector(Of String)()
            End If
            If o.instance() Is Nothing Then
                o.setinstance(New vector(Of String)())
            Else
                o.instance().clear()
            End If

            If IO.Directory.Exists(i) Then
                Dim ss() As String = Nothing
                ss = listfiles(i)
                o.instance().push_back(ss)
                ss = listdirectories(i)
                o.instance().push_back(ss)
            End If
            Return True
        End Function

        Protected Overrides Function finder() As iVariableFinder(Of String)
            Return string_finder()
        End Function

        Protected Overrides Function returnType() As typeDefination
            Return typeDefination.vector
        End Function

        Protected Overrides Function elementType1() As typeDefination
            Return typeDefination.string
        End Function
    End Class

    'parameters, src, any, convert to string, the source directory
    '            dest, any, convert to string, the destination directory
    '            overwrite, bool, optional, whether overwrite existing files, default value is false
    '            ignoreEmptyDirectory, bool, optional, whether ignore empty directories, default value is false
    Public Class directorycopy
        Inherits internalFunction

        Public Class parameters
            Public Const src As String = "src"
            Public Const dest As String = "dest"
            Public Const overwrite As String = "overwrite"
            Public Const ignoreEmptyDirectory As String = "ignoreEmptyDirectory"
        End Class

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.src, parameters.dest, parameters.overwrite, parameters.ignoreEmptyDirectory)
        End Sub

        Public Sub New()
            MyBase.New("directory.copy", m)
        End Sub

        Protected Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
            Dim src As String = Nothing
            If ctx.findVariableStr(parameters.src, src) Then
                Dim dest As String = Nothing
                If ctx.findVariableStr(parameters.dest, dest) Then
                    Dim overwrite As Boolean
                    If Not ctx.findVariableBool(parameters.overwrite, overwrite) Then
                        overwrite = False
                    End If
                    Dim ignoreEmptyDirectory As Boolean
                    If Not ctx.findVariableBool(parameters.ignoreEmptyDirectory, ignoreEmptyDirectory) Then
                        ignoreEmptyDirectory = False
                    End If

                    output = New bare_variable(typeDefination.bool, _
                                               copyDirectory(src, dest, _
                                                             ignore_empty_directory:=ignoreEmptyDirectory, _
                                                             overwrite:=overwrite))
                    Return True
                Else
                    raiseError("expect parameter " + parameters.dest, errorHandle.errorType.user)
                    Return False
                End If
            Else
                raiseError("expect parameter " + parameters.src, errorHandle.errorType.user)
                Return False
            End If
        End Function
    End Class

    Public Class filedelete
        Inherits inputIFunction(Of String, Boolean)

        Public Sub New()
            MyBase.New("file.delete")
        End Sub

        Protected Overrides Function finder() As iVariableFinder(Of String)
            Return string_finder()
        End Function

        Protected Overrides Function invoke(ByVal i As String, ByRef o As Boolean) As Boolean
            Try
                IO.File.Delete(i)
                o = True
            Catch
                o = False
            End Try

            Return True
        End Function

        Protected Overrides Function returnType() As typeDefination
            Return typeDefination.bool
        End Function
    End Class

    Public Class _filename
        Inherits inputIFunction(Of String, String)

        Public Sub New()
            MyBase.New("file.filename")
        End Sub

        Protected Overrides Function finder() As iVariableFinder(Of String)
            Return string_finder()
        End Function

        Protected Overrides Function invoke(ByVal i As String, ByRef o As String) As Boolean
            o = filename(i)
            Return True
        End Function

        Protected Overrides Function returnType() As typeDefination
            Return typeDefination.string
        End Function
    End Class

    Public Class _parentpath
        Inherits inputIFunction(Of String, String)

        Public Sub New()
            MyBase.New("directory.parentpath")
        End Sub

        Protected Overrides Function finder() As iVariableFinder(Of String)
            Return string_finder()
        End Function

        Protected Overrides Function invoke(ByVal i As String, ByRef o As String) As Boolean
            o = parentpath(i)
            Return True
        End Function

        Protected Overrides Function returnType() As typeDefination
            Return typeDefination.string
        End Function
    End Class

    Public Class _filesize
        Inherits inputIOutputOFunction(Of String, Int64)

        Public Sub New()
            MyBase.new("file.size")
        End Sub

        Protected Overrides Function finder() As iVariableFinder(Of String)
            Return string_finder()
        End Function

        Protected Overrides Function outputType() As typeDefination
            Return typeDefination.int
        End Function

        Protected Overrides Function invoke(ByVal i As String, ByRef o As Int64) As Boolean
            Return filesize(i, o)
        End Function
    End Class
End Namespace